package com.ts.api.http.platform.biz;

import com.alibaba.fastjson.JSONObject;
import com.gitee.apanlh.util.algorithm.encrypt.symmetric.AES;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.MapUtils;
import com.gitee.apanlh.util.dataformat.JsonUtils;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.ts.api.http.platform.entity.apply.ApiPlatFormApplyLiTongData;
import com.ts.api.http.platform.entity.dataexchange.ApiPlatDataExchangeLiTongData;
import com.ts.api.module.api.convert.core.annotation.ApiPlan;
import com.ts.api.module.api.convert.core.annotation.ApiPlanConvertType;
import com.ts.api.module.api.convert.core.entity.ApiApplyParameter;
import com.ts.api.module.api.convert.core.entity.ApiBizStatus;
import com.ts.api.module.api.convert.core.entity.ApiDataExchangeParameter;
import com.ts.api.module.api.convert.platform.entity.RespPlatformNoticeApply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5Apply;
import com.ts.api.module.api.convert.platform.entity.RespPlatformPhoneMd5DataExchange;
import com.ts.api.module.api.convert.platform.handler.ApiPlatformPhoneMd5ConvertHandler;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 安心出离瞳全API28分润
 *
 * 文档：https://www.showdoc.com.cn/2467420540343640 密码：123456
 *
 * @author WJB
 */
@ApiPlan(productId = "325", type = ApiPlanConvertType.PLAT_PHONE_MD5)
public class ApiPlatLiTong28Convert implements ApiPlatformPhoneMd5ConvertHandler {

    private static String KEY = "Qu7HGF8TUSYoE9eZ";
    private static String CHANNEL_CODE = "60020491";

    @Override
    public Object dataExchange(ApiDataExchangeParameter ApiDataChangeMessage) {
        ApiPlatDataExchangeLiTongData apiPlatDataExchangeLiTongData = new ApiPlatDataExchangeLiTongData();

        apiPlatDataExchangeLiTongData.setChannel(CHANNEL_CODE);
        String time = String.valueOf(System.currentTimeMillis());
        apiPlatDataExchangeLiTongData.setTimestamp(time);
        apiPlatDataExchangeLiTongData.setPhonemd5(ApiDataChangeMessage.getPhoneMd5());
        apiPlatDataExchangeLiTongData.setName("浩安星");
        apiPlatDataExchangeLiTongData.setCity(ApiDataChangeMessage.getCity());
        apiPlatDataExchangeLiTongData.setAge(Integer.valueOf(ApiDataChangeMessage.getAge()));
        // sex 性别 1男2女
        String sex = ApiDataChangeMessage.getSex();
        Integer sexChoose = ChooseEq.create()
                .when(sex, "0", 1)
                .when(sex, "1", 2)
                .end(1);
        apiPlatDataExchangeLiTongData.setSex(sexChoose);
        // loanAmount 预贷款额度
        String loanAmount = ApiDataChangeMessage.getLoanAmount();
        String money = ChooseEq.create()
                .when(loanAmount, "1", "30000")
                .when(loanAmount, "2", "50000")
                .when(loanAmount, "3", "100000")
                .when(loanAmount, "4", "200000")
                .end("30000");
        apiPlatDataExchangeLiTongData.setLoan(money);

        //  loanTerm 贷款期限
        String loanTerm = ApiDataChangeMessage.getLoanTerm();
        Integer Term = ChooseEq.create()
                .when(loanTerm, "1", 1)
                .when(loanTerm, "2", 2)
                .when(loanTerm, "3", 3)
                .when(loanTerm, "4", 4)
                .when(loanTerm, "5", 5)
                .when(loanTerm, "6", 6)
                .end(1);
        apiPlatDataExchangeLiTongData.setLoan_term(Term);

        // loan_use 贷款用途
        String loanPurpose = ApiDataChangeMessage.getLoanPurpose();
        Integer loan_use = ChooseEq.create()
                .when(loanPurpose, "1", 5)
                .when(loanPurpose, "2", 4)
                .when(loanPurpose, "3", 8)
                .when(loanPurpose, "4", 3)
                .when(loanPurpose, "5", 8)
                .when(loanPurpose, "6", 6)
                .end(6);
        apiPlatDataExchangeLiTongData.setLoan_use(loan_use);

        // education 学历
        String education = ApiDataChangeMessage.getEducationAttainment();
        Integer edu = ChooseEq.create()
                .when(education, "1", 1)
                .when(education, "2", 2)
                .when(education, "3", 3)
                .when(education, "4", 4)
                .when(education, "5", 5)
                .when(education, "6", 6)
                .end(1);
        apiPlatDataExchangeLiTongData.setEdu(edu);

        // zhima 芝麻分:
        String sesameCredit = ApiDataChangeMessage.getSesameCredit();
        Integer zhima = ChooseEq.create()
                .when(sesameCredit, "1", 2)
                .when(sesameCredit, "2", 3)
                .when(sesameCredit, "3", 4)
                .when(sesameCredit, "4", 5)
                .end(1);
        apiPlatDataExchangeLiTongData.setZhima(zhima);

        //  花呗
        String huaBei = ApiDataChangeMessage.getHbLimit();
        Integer huaBeiLT = ChooseEq.create()
                .when(huaBei, "1", 1)
                .when(huaBei, "2", 2)
                .when(huaBei, "3", 3)
                .when(huaBei, "4", 4)
                .when(huaBei, "5", 4)
                .end(1);
        apiPlatDataExchangeLiTongData.setHuabei(huaBeiLT);

        //  白条
        String baiTiao = ApiDataChangeMessage.getBtLimit();
        Integer baiTiaoLT = ChooseEq.create()
                .when(baiTiao, "1", 1)
                .when(baiTiao, "2", 2)
                .when(baiTiao, "3", 3)
                .when(baiTiao, "4", 3)
                .when(baiTiao, "5", 3)
                .end(1);
        apiPlatDataExchangeLiTongData.setBaitiao(baiTiaoLT);

        // fund 公积金:
        String providentFund = ApiDataChangeMessage.getProvidentFund();
        Integer fund = ChooseEq.create()
                .when(providentFund, "1", 1)
                .when(providentFund, "2", 2)
                .when(providentFund, "3", 2)
                .when(providentFund, "4", 2)
                .end(1);
        apiPlatDataExchangeLiTongData.setFund(fund);

        // has 社保：
        String socialSecurity = ApiDataChangeMessage.getSocialSecurity();
        Integer has = ChooseEq.create()
                .when(socialSecurity, "1", 1)
                .when(socialSecurity, "2", 2)
                .when(socialSecurity, "3", 2)
                .when(socialSecurity, "4", 2)
                .end(2);
        apiPlatDataExchangeLiTongData.setHas(has);

        // vehicle  车产：
        String carInfo = ApiDataChangeMessage.getCarInfo();
        Integer car = ChooseEq.create()
                .when(carInfo, "1", 1)
                .when(carInfo, "2", 2)
                .when(carInfo, "3", 2)
                .when(carInfo, "4", 2)
                .end(1);
        apiPlatDataExchangeLiTongData.setCar(car);

        // house 房产：
        String realEstateInfo = ApiDataChangeMessage.getRealEstateInfo();
        Integer house = ChooseEq.create()
                .when(realEstateInfo, "1", 1)
                .when(realEstateInfo, "2", 2)
                .when(realEstateInfo, "3", 2)
                .when(realEstateInfo, "4", 2)
                .when(realEstateInfo, "5", 2)
                .when(realEstateInfo, "6", 2)
                .end(1);
        apiPlatDataExchangeLiTongData.setHouse(house);

        // insurance 保单保险:
        String insurancePolicy = ApiDataChangeMessage.getInsurancePolicy();
        Integer insurance = ChooseEq.create()
                .when(insurancePolicy, "1", 1)
                .when(insurancePolicy, "2", 2)
                .when(insurancePolicy, "3", 2)
                .when(insurancePolicy, "4", 2)
                .end(1);
        apiPlatDataExchangeLiTongData.setInsurance(insurance);

        // overdue 逾期状态:
        String overdue = ApiDataChangeMessage.getOverdue();
        Integer overdueLT = ChooseEq.create()
                .when(overdue, "1", 1)
                .end(2);
        apiPlatDataExchangeLiTongData.setOverdue(overdueLT);

        // creditCard 信用卡
        String creditCard = ApiDataChangeMessage.getCreditLimit();
        Integer credit = ChooseEq.create()
                .when(creditCard, "1", 1)
                .when(creditCard, "2", 2)
                .when(creditCard, "3", 2)
                .when(creditCard, "4", 2)
                .when(creditCard, "5", 2)
                .when(creditCard, "6", 2)
                .end(1);
        apiPlatDataExchangeLiTongData.setCredit(credit);

        // working 职业身份
        String career = ApiDataChangeMessage.getCareer();
        Integer working = ChooseEq.create()
                .when(career, "1", 1)
                .when(career, "2", 3)
                .when(career, "3", 2)
                .when(career, "4", 5)
                .when(career, "5", 4)
                .end(1);
        apiPlatDataExchangeLiTongData.setWorking(working);

        //  是否有企业
        Integer company = ChooseEq.create()
                .when(career, "3", 2)
                .end(1);
        apiPlatDataExchangeLiTongData.setCompany(company);

        //  客户IP
        apiPlatDataExchangeLiTongData.setJoinip(ApiDataChangeMessage.getIp());
        //  微粒贷
        apiPlatDataExchangeLiTongData.setMicro_loan(1);

        AES aes = AES.createEcb(KEY.getBytes());
        String jsonStr = JsonUtils.toJson(apiPlatDataExchangeLiTongData);
        String dataStr = aes.encryptToBase64Str(jsonStr);
        Map<Object, Object> map = MapUtils.newHashMap();
        map.put("bizParam", dataStr);
        map.put("channel", CHANNEL_CODE);
        map.put("timestamp", time);
        return JsonUtils.toJson(map);
    }

    @Override
    public String apply(ApiApplyParameter ApiApplyMessage) {

        ApiPlatFormApplyLiTongData apiPlatFormApplyLiTongData = new ApiPlatFormApplyLiTongData();
        apiPlatFormApplyLiTongData.setChannel(CHANNEL_CODE);
        apiPlatFormApplyLiTongData.setTimestamp(String.valueOf(System.currentTimeMillis()));
        apiPlatFormApplyLiTongData.setPhone(ApiApplyMessage.getPhone());
        apiPlatFormApplyLiTongData.setName(ApiApplyMessage.getUserName());
        apiPlatFormApplyLiTongData.setCity(ApiApplyMessage.getCity());
        apiPlatFormApplyLiTongData.setAge(Integer.valueOf(ApiApplyMessage.getAge()));
        // sex 性别 1男2女
        String sex = ApiApplyMessage.getSex();
        Integer sexChoose = ChooseEq.create()
                .when(sex, "0", 1)
                .when(sex, "1", 2)
                .end(1);
        apiPlatFormApplyLiTongData.setSex(sexChoose);
        // loanAmount 预贷款额度
        String loanAmount = ApiApplyMessage.getLoanAmount();
        String money = ChooseEq.create()
                .when(loanAmount, "1", "30000")
                .when(loanAmount, "2", "50000")
                .when(loanAmount, "3", "100000")
                .when(loanAmount, "4", "200000")
                .end("30000");
        apiPlatFormApplyLiTongData.setLoan(money);

        //  loanTerm 贷款期限
        String loanTerm = ApiApplyMessage.getLoanTerm();
        Integer Term = ChooseEq.create()
                .when(loanTerm, "1", 1)
                .when(loanTerm, "2", 2)
                .when(loanTerm, "3", 3)
                .when(loanTerm, "4", 4)
                .when(loanTerm, "5", 5)
                .when(loanTerm, "6", 6)
                .end(1);
        apiPlatFormApplyLiTongData.setLoan_term(Term);

        // loan_use 贷款用途
        String loanPurpose = ApiApplyMessage.getLoanPurpose();
        Integer loan_use = ChooseEq.create()
                .when(loanPurpose, "1", 5)
                .when(loanPurpose, "2", 4)
                .when(loanPurpose, "3", 8)
                .when(loanPurpose, "4", 3)
                .when(loanPurpose, "5", 8)
                .when(loanPurpose, "6", 6)
                .end(6);
        apiPlatFormApplyLiTongData.setLoan_use(loan_use);

        // education 学历
        String education = ApiApplyMessage.getEducationAttainment();
        Integer edu = ChooseEq.create()
                .when(education, "1", 1)
                .when(education, "2", 2)
                .when(education, "3", 3)
                .when(education, "4", 4)
                .when(education, "5", 5)
                .when(education, "6", 6)
                .end(1);
        apiPlatFormApplyLiTongData.setEdu(edu);

        // zhima 芝麻分:
        String sesameCredit = ApiApplyMessage.getSesameCredit();
        Integer zhima = ChooseEq.create()
                .when(sesameCredit, "1", 2)
                .when(sesameCredit, "2", 3)
                .when(sesameCredit, "3", 4)
                .when(sesameCredit, "4", 5)
                .end(1);
        apiPlatFormApplyLiTongData.setZhima(zhima);

        //  花呗
        String huaBei = ApiApplyMessage.getHbLimit();
        Integer huaBeiLT = ChooseEq.create()
                .when(huaBei, "1", 1)
                .when(huaBei, "2", 2)
                .when(huaBei, "3", 3)
                .when(huaBei, "4", 4)
                .when(huaBei, "5", 4)
                .end(1);
        apiPlatFormApplyLiTongData.setHuabei(huaBeiLT);

        //  白条
        String baiTiao = ApiApplyMessage.getBtLimit();
        Integer baiTiaoLT = ChooseEq.create()
                .when(baiTiao, "1", 1)
                .when(baiTiao, "2", 2)
                .when(baiTiao, "3", 3)
                .when(baiTiao, "4", 3)
                .when(baiTiao, "5", 3)
                .end(1);
        apiPlatFormApplyLiTongData.setBaitiao(baiTiaoLT);

        // fund 公积金:
        String providentFund = ApiApplyMessage.getProvidentFund();
        Integer fund = ChooseEq.create()
                .when(providentFund, "1", 1)
                .when(providentFund, "2", 2)
                .when(providentFund, "3", 2)
                .when(providentFund, "4", 2)
                .end(1);
        apiPlatFormApplyLiTongData.setFund(fund);

        // has 社保：
        String socialSecurity = ApiApplyMessage.getSocialSecurity();
        Integer has = ChooseEq.create()
                .when(socialSecurity, "1", 1)
                .when(socialSecurity, "2", 2)
                .when(socialSecurity, "3", 2)
                .when(socialSecurity, "4", 2)
                .end(2);
        apiPlatFormApplyLiTongData.setHas(has);

        // vehicle  车产：
        String carInfo = ApiApplyMessage.getCarInfo();
        Integer car = ChooseEq.create()
                .when(carInfo, "1", 1)
                .when(carInfo, "2", 2)
                .when(carInfo, "3", 2)
                .when(carInfo, "4", 2)
                .end(1);
        apiPlatFormApplyLiTongData.setCar(car);

        // house 房产：
        String realEstateInfo = ApiApplyMessage.getRealEstateInfo();
        Integer house = ChooseEq.create()
                .when(realEstateInfo, "1", 1)
                .when(realEstateInfo, "2", 2)
                .when(realEstateInfo, "3", 2)
                .when(realEstateInfo, "4", 2)
                .when(realEstateInfo, "5", 2)
                .when(realEstateInfo, "6", 2)
                .end(1);
        apiPlatFormApplyLiTongData.setHouse(house);

        // insurance 保单保险:
        String insurancePolicy = ApiApplyMessage.getInsurancePolicy();
        Integer insurance = ChooseEq.create()
                .when(insurancePolicy, "1", 1)
                .when(insurancePolicy, "2", 2)
                .when(insurancePolicy, "3", 2)
                .when(insurancePolicy, "4", 2)
                .end(1);
        apiPlatFormApplyLiTongData.setInsurance(insurance);

        // overdue 逾期状态:
        String overdue = ApiApplyMessage.getOverdue();
        Integer overdueLT = ChooseEq.create()
                .when(overdue, "1", 1)
                .end(2);
        apiPlatFormApplyLiTongData.setOverdue(overdueLT);

        // creditCard 信用卡
        String creditCard = ApiApplyMessage.getCreditLimit();
        Integer credit = ChooseEq.create()
                .when(creditCard, "1", 1)
                .when(creditCard, "2", 2)
                .when(creditCard, "3", 2)
                .when(creditCard, "4", 2)
                .when(creditCard, "5", 2)
                .when(creditCard, "6", 2)
                .end(1);
        apiPlatFormApplyLiTongData.setCredit(credit);

        // working 职业身份
        String career = ApiApplyMessage.getCareer();
        Integer working = ChooseEq.create()
                .when(career, "1", 1)
                .when(career, "2", 3)
                .when(career, "3", 2)
                .when(career, "4", 5)
                .when(career, "5", 4)
                .end(1);
        apiPlatFormApplyLiTongData.setWorking(working);

        //  是否有企业
        Integer company = ChooseEq.create()
                .when(career, "3", 2)
                .end(1);
        apiPlatFormApplyLiTongData.setCompany(company);

        //  客户IP
        apiPlatFormApplyLiTongData.setJoinip(ApiApplyMessage.getIp());
        //  微粒贷
        apiPlatFormApplyLiTongData.setMicro_loan(1);

        AES aes = AES.createEcb(KEY.getBytes());
        String jsonStr = JsonUtils.toJson(apiPlatFormApplyLiTongData);
        String dataStr = aes.encryptToBase64Str(jsonStr);
        Map<Object, Object> map = MapUtils.newHashMap();
        map.put("bizParam", dataStr);
        map.put("channel", CHANNEL_CODE);
        map.put("timestamp", apiPlatFormApplyLiTongData.getTimestamp());
        return JsonUtils.toJson(map);
    }

    @Override
    public RespPlatformPhoneMd5Apply respApply(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        if (!Eq.object(jsonObject.getInteger("code"), 100)) {
            return RespPlatformPhoneMd5Apply.ofFail();
        }
        return RespPlatformPhoneMd5Apply.ofSuc();
    }

    @Override
    public RespPlatformPhoneMd5DataExchange respDataExchange(String respJson) {
        JSONObject jsonObject = JsonUtils.get(respJson);
        if (jsonObject == null) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }
        if(!Eq.object(jsonObject.getInteger("code"), 100)) {
            return RespPlatformPhoneMd5DataExchange.ofFail();
        }

        JSONObject resData = JsonUtils.get(respJson).getJSONObject("data");

        BigDecimal price = resData.getBigDecimal("price");
        String companyName = resData.getString("companyName");
        String productName = resData.getString("productName");

        RespPlatformPhoneMd5DataExchange result = RespPlatformPhoneMd5DataExchange.ofSuc();
        result.setPrice(price);
        result.setDisplayName(companyName);
        result.setProductName(productName);
        return result;
    }

    @Override
    public RespPlatformNoticeApply parseApplyNotice(RequestVO requestVO) {
        return null;
    }

    @Override
    public Map<String, Object> respApplyNotice(ApiBizStatus apiBizStatus) {
        return Map.of();
    }

}
